cd d:\tex\research\Dian\THDL
log using Table_3.log, replace

insheet using "D:\tex\research\Dian\THDL\chuan_coded_2020_new_interest_bao-fixed.csv"

tabulate contract
drop if contract!=1

* drop if not in Taiwan
*
replace tien = "������ë������" if tien == "������ë���?��" 
replace tien = "�u�{�x�W�i�h�c�a�ڤ��׽s" if tien == "�u�{�x�W�i?�c�a��?��׽s" 
drop if tien == "������ë������" | tien == "�u�{�x�W�i�h�c�a�ڤ��׽s" | tien =="Not in Taiwan"

destring trade, replace
tabulate trade
list no_1 no_2 no_3 if trade == 0
list no_1 no_2 no_3 if trade == 1.3
list no_1 no_2 no_3 if trade == 14.5
list no_1 no_2 no_3 if trade == 3.5
list no_1 no_2 no_3 if trade == 5.7

destring year, replace
drop if year > 1895

* Unicode-Big5 already applied, otherwise the following replacement won't work.
*
replace tien = "�Q����" if tien == "?����" | tien == "�Q?��"
replace tien = "�����d" if tien == "����?" | tien == "�f���d" | tien == "�f���d�U"
replace tien = "�O�n" if tien == "�x�n"
replace tien = "�O�_" if tien == "�x�_"
replace tien = "�O��" if tien == "�x��"
replace tien = "�O�F" if tien == "�x�F"
replace tien = "��J��" if tien == "���"
replace tien = "�Ÿq" if tien == "�Ÿq�U"
replace tien = "�]��" if tien == "�]��?"
replace tien = "��" if tien == "��?"
tabulate tien

generate tien_1=tien=="�y��"
generate tien_2=tien=="��"
generate tien_3=tien=="�`�|"
generate tien_4=tien=="�O�_"
generate tien_5=tien=="��J��"
generate tien_6=tien=="�s��"
generate tien_7=tien=="�]��"
generate tien_8=tien=="�O��"
generate tien_9=tien=="����"
generate tien_10=tien=="�n��"
generate tien_11=tien=="�椻"
generate tien_12=tien=="�Ÿq"
generate tien_13=tien=="�Q����"
generate tien_14=tien=="�O�n"
generate tien_15=tien=="��s"
generate tien_16=tien=="�����d"
generate tien_17=tien=="���U"
generate tien_18=tien=="���K"

replace money_unit_1 = "��" if money_unit_1 == "?"
replace money_unit_1 = "��" if money_unit_1 == "��?"
replace money_unit_1 = "��" if money_unit_1 == "��?" | money_unit_1 == "��"
replace money_unit_1 = "��" if money_unit_1 == "��?"
replace money_unit_1 = "�j��" if money_unit_1 == "�j��?" | money_unit_1 == "�j��"
replace money_unit_1 = "�j��" if money_unit_1 == "�j?"
replace money_unit_1 = "�j��" if money_unit_1 == "?��" | money_unit_1 == "�j��?"
replace money_unit_1 = "�d��" if money_unit_1 == "�d��?" | money_unit_1 == "�d ��"

gen money_unit_1_new = -9
replace money_unit_1_new = 1 if money_unit_1 == "�j��" | money_unit_1 == "�j��" | money_unit_1 == "�j��" | money_unit_1 == "��" | money_unit_1 == "��" | money_unit_1 == "��"
replace money_unit_1_new = 2 if money_unit_1 == "��" | money_unit_1 == "�⥫�m" | money_unit_1 == "�����Ȩ�"
replace money_unit_1_new = 3 if money_unit_1 == "�d��" | money_unit_1 == "�a��"
replace money_unit_1_new = 4 if money_unit_1 == "��"


replace land_nation = 0 if land_nation >=.
replace chi_wei = 0 if chi_wei >=.
replace certificate_others = 0 if certificate_others >=.
replace certificate_cultivation = 0 if certificate_cultivation >=.
replace measure = 0 if measure >=.

tabulate year trade if trade ==1 | trade==1.5 | trade ==2, matrow(m_year) matcell(m_trade) matcol(m_col)
tabulate year trade if trade== 7 | trade==14 | trade ==3 | trade==5 | trade==4 | trade ==8 | trade==5.5, matrow(m_year_b) matcell(m_trade_b) matcol(m_col_b)

tabulate land trade if land >=0 & trade <=2 & trade >0
tabulate irrigation trade if land >=0 & trade <=2 & trade >0

tabulate seller_adjacent trade if trade <=2 & trade >0
tabulate buyer_adjacent trade if trade <=2 & trade >0

generate dian=trade==2
generate paddy=land==1
generate paddy_2=land==2
replace irrigation=-9 if irrigation>=.
generate paddy_irrig = paddy * irrigation

replace tax_cane_cart_yuan = -100 if tax_cane_cart_yuan >=.
replace tax_cane_cart_liang = -100 if tax_cane_cart_liang >=.
replace tax_1_liang = -100 if tax_1_liang >=.
replace tax_1_yuan = -100 if tax_1_yuan >=.
replace tax_1_dan = -100 if tax_1_dan >=.
replace tax_2_dan = -100 if tax_2_dan >=.
replace tax_2_liang = -100 if tax_2_liang >=.
replace tax_depreciation_liang = -100 if tax_depreciation_liang >=.
generate tax = 0
replace tax = 1 if tax_cane_cart_yuan > -50 | tax_cane_cart_liang > -50 | tax_1_liang > -50 | tax_1_yuan > -50 | tax_1_dan > -50 | tax_2_dan > -50 | tax_2_liang > -50 | tax_depreciation_liang > -50
generate tax_measure = 0
replace tax_measure = 1 if tax == 1 | measure == 1

replace w_rb = 0 if tien =="�]��" & bao =="�ǪF�W"
replace e_purple = 1 if tien =="�]��" & bao =="�ǪF�W"
replace w_rb = 1 if bao =="�j�{�W" | bao =="�j�{��" | bao =="�j�{�U" 
replace e_purple = 0 if bao =="�j�{�W" | bao =="�j�{��" | bao =="�j�{�U" 
replace w_rb = 1 if tien == "����"
replace e_purple = 0 if tien == "����"
replace w_rb = 0 if tien =="�n��" & (bao =="�_���" | bao =="�H����" | bao =="����")
replace e_purple = 1 if tien =="�n��" & (bao =="�_���" | bao =="�H����" | bao =="����")
replace w_rb = 1 if tien =="�椻" & (bao =="�ˬw" | bao =="����" | bao =="����" | bao =="�ըF�[" | bao =="�j����" | bao =="�L����" | bao =="���ߥ_")
replace e_purple = 0 if tien == "�椻" & (bao =="�ˬw" | bao =="����" | bao =="����" | bao =="�ըF�[" | bao =="�j����" | bao =="�L����" | bao =="���ߥ_")
replace w_rb = 1 if tien =="�Ÿq" & (bao =="���ߥ_" | bao =="�j�d�}��" | bao =="���Y��" | bao =="���ߦ�" | bao =="���߫n" | bao =="���ߪF�U" | bao =="�Ÿq��" | bao =="���Y��" | bao =="�j�d�}�F�U" | bao =="���J��" | bao =="�j���Ц�")
replace e_purple = 0 if tien =="�Ÿq" & (bao =="���ߥ_" | bao =="�j�d�}��" | bao =="���Y��" | bao =="���ߦ�" | bao =="���߫n" | bao =="���ߪF�U" | bao =="�Ÿq��" | bao =="���Y��" | bao =="�j�d�}�F�U" | bao =="���J��" | bao =="�j���Ц�")
replace w_rb = 1 if tien =="�Q����" & (bao == "�U�XФ�n" | bao =="�U�XФ�_" | bao =="�G�o���")
replace e_purple = 0 if tien =="�Q����" & (bao == "�U�XФ�n" | bao =="�U�XФ�_" | bao =="�G�o���")
replace w_rb = 1 if tien =="�Q����" & (bao == "�G�ݫ�" | bao =="�K�u��" | bao =="�Ӥl�c" | bao =="�Q����" | bao =="�s���" | bao == "��Ž����" | bao =="�ǥ�" | bao =="�Ψ���" | bao =="���^" | bao =="�T�����" | bao =="�T����F" | bao =="���s" | bao =="���ƨ���" | bao =="���ƨ��F" | bao =="�j���Ц�")
replace e_purple = 0 if tien =="�Q����" & (bao == "�G�ݫ�" | bao =="�K�u��" | bao =="�Ӥl�c" | bao =="�Q����" | bao =="�s���" | bao == "��Ž����" | bao =="�ǥ�" | bao =="�Ψ���" | bao =="���^" | bao =="�T�����" | bao =="�T����F" | bao =="���s" | bao =="���ƨ���" | bao =="���ƨ��F" | bao =="�j���Ц�")
replace w_rb = 1 if tien =="�O�n" & bao != "�~�s�ƫn��" & bao != "����P�˦訽" & bao !="���s�ƫn��"
replace e_purple = 0 if tien =="�O�n" & bao != "�~�s�ƫn��" & bao != "����P�˦訽" & bao !="���s�ƫn��"
replace w_rb = 0 if tien =="�O�n" & bao =="���s�ƫn��"
replace e_purple = 0 if tien =="�O�n" & bao =="���s�ƫn��"
replace w_rb = 1 if tien =="��s"
replace e_purple = 0 if tien =="��s"
replace w_rb = 1 if tien =="�����d" & (bao =="�Ų�����" | bao =="�R�w�F��")
replace e_purple = 0 if tien =="�����d" & (bao =="�Ų�����" | bao =="�R�w�F��")
replace w_rb = 1 if tien =="���U" & (bao =="���W��" | bao =="���U��" | bao =="�s�騽" | bao =="��F����" | bao =="��F�U��")
replace e_purple = 0 if tien =="���U" & (bao =="���W��" | bao =="���U��" | bao =="�s�騽" | bao =="��F����" | bao =="��F�U��")
replace w_rb = 0 if tien =="���K"
replace e_purple = 1 if tien =="���K"

replace w_rb = -9 if w_rb >=.
replace e_purple = -9 if e_purple >=.
replace distance_rb = -9 if distance_rb >=.
replace distance_purple = -9 if distance_purple >=.

replace buyer_adjacent=-9 if buyer_adjacent >=.
replace aboriginal_seller=-9 if aboriginal_seller >=.
replace interest1905=-9 if interest1905 >=.





generate year_before_1790 = 0
replace year_before_1790 = -9 if year >=. | year == -9
replace year_before_1790 = 1 if year <=1789 & year !=-9
generate year_after_1850 = 0
replace year_after_1850 = -9 if  year >=. | year == -9
replace year_after_1850 = 1 if year >=1850 & year <.


drop if tien != "�y��" & tien != "��" & tien != "�`�|" & tien != "�O�_" & tien != "��J��" & tien != "�s��" & tien != "�]��" & tien != "�O��" & tien != "����" & tien != "�n��" & tien != "�椻" & tien != "�Ÿq" & tien != "�Q����" & tien != "�O�n" &  tien != "��s" & tien != "�����d" & tien !=  "���U" & tien != "���K"      


drop if tien =="-9"
drop if year == -9

gen decade = floor(year/10)

bysort tien decade: egen measure_avg_2 = mean(measure) if measure != -9
bysort tien decade: egen certificate_cultivation_avg_2 = mean(certificate_cultivation) if certificate_cultivation != -9 
bysort tien decade: egen certificate_others_avg_2 = mean(certificate_others) if certificate_others != -9 
bysort tien decade: egen chi_wei_avg_2 = mean(chi_wei) if chi_wei != -9 


* local governance -- bao_decade average 
drop if bao_code == -9
bysort bao_code decade: egen measure_avg_3 = mean(measure) if measure != -9
bysort bao_code decade: egen certificate_cultivation_avg_3 = mean(certificate_cultivation) if certificate_cultivation != -9 
bysort bao_code decade: egen certificate_others_avg_3 = mean(certificate_others) if certificate_others != -9 
bysort bao_code decade: egen chi_wei_avg_3 = mean(chi_wei) if chi_wei != -9 

* (1)
logit dian paddy_irrig seller_adjacent buyer_adjacent previous_owner measure_avg_3 chi_wei_avg_3 certificate_others_avg_3 certificate_cultivation_avg_3 land_nation if trade <=2 & trade >0 & seller_adjacent !=-9 & buyer_adjacent !=-9 & land!=-9 & land <=2 & previous_owner !=-9 & previous_owner !=9 & certificate_others !=-9 & chi_wei !=-9 & certificate_cultivation !=-9 & measure !=-9 & land_nation !=-9 & irrigation !=-9 & tien!="-9"
outreg2 using logit_out_governance_all_4_bao.doc, replace

* (2) borders added
logit dian e_purple w_rb paddy_irrig seller_adjacent buyer_adjacent previous_owner measure_avg_3 chi_wei_avg_3 certificate_others_avg_3 certificate_cultivation_avg_3 land_nation if e_purple !=-9 & w_rb !=-9 & trade <=2 & trade >0 & seller_adjacent !=-9 & buyer_adjacent !=-9 & land!=-9 & land <=2 & previous_owner !=-9 & previous_owner !=9 & certificate_others !=-9 & chi_wei !=-9 & certificate_cultivation !=-9 & measure !=-9 & land_nation !=-9 & irrigation !=-9 & tien!="-9"
outreg2 using logit_out_governance_all_4_bao.doc, append

* (3) aboriginal_sellers
logit dian e_purple w_rb paddy_irrig seller_adjacent buyer_adjacent previous_owner measure_avg_3 chi_wei_avg_3 certificate_others_avg_3 certificate_cultivation_avg_3 land_nation aboriginal_seller if aboriginal_seller !=-9 & e_purple !=-9 & w_rb !=-9 & trade <=2 & trade >0 & seller_adjacent !=-9 & buyer_adjacent !=-9 & land!=-9 & land <=2 & previous_owner !=-9 & previous_owner !=9 & certificate_others !=-9 & chi_wei !=-9 & certificate_cultivation !=-9 & measure !=-9 & land_nation !=-9 & irrigation !=-9 & tien!="-9"
outreg2 using logit_out_governance_all_4_bao.doc, append

* (4) tien and year controlled
logit dian paddy_irrig seller_adjacent buyer_adjacent previous_owner measure_avg_3 chi_wei_avg_3 certificate_others_avg_3 certificate_cultivation_avg_3 land_nation e_purple w_rb aboriginal_seller tien_* year_before_1790 year_after_1850 if aboriginal_seller !=-9  & trade <=2 & trade >0 & seller_adjacent !=-9 & buyer_adjacent !=-9 & land!=-9 & land <=2 & previous_owner !=-9 & previous_owner !=9 & certificate_others !=-9 & chi_wei !=-9 & certificate_cultivation !=-9 & measure !=-9 & land_nation !=-9 & w_rb !=-9 & e_purple !=-9 & irrigation !=-9 & year_before_1790 !=-9 & year_after_1850 !=-9 & tien!="-9"
outreg2 using logit_out_governance_all_4_bao.doc, append

* local governance -- bao_decade average + interest
* (5) tien and year not controlled
logit dian interest1905 e_purple w_rb paddy_irrig seller_adjacent buyer_adjacent previous_owner measure_avg_3 chi_wei_avg_3 certificate_others_avg_3 certificate_cultivation_avg_3 land_nation aboriginal_seller if aboriginal_seller !=-9 & e_purple !=-9 & w_rb !=-9 & trade <=2 & trade >0 & seller_adjacent !=-9 & buyer_adjacent !=-9 & land!=-9 & land <=2 & previous_owner !=-9 & previous_owner !=9 & certificate_others !=-9 & chi_wei !=-9 & certificate_cultivation !=-9 & measure !=-9 & land_nation !=-9 & irrigation !=-9 & tien!="-9" & interest1905 !=-9
outreg2 using logit_out_governance_all_3_interest_bao.doc, replace

sum dian paddy_irrig seller_adjacent buyer_adjacent previous_owner measure_avg_3 certificate_cultivation_avg_3 certificate_others_avg_3 chi_wei_avg_3 land_nation e_purple w_rb aboriginal_seller interest1905 if e_purple !=-9 & w_rb !=-9 & trade <=2 & trade >0 & seller_adjacent !=-9 & buyer_adjacent !=-9 & land!=-9 & land <=2 & previous_owner !=-9 & previous_owner !=9 & certificate_others !=-9 & chi_wei !=-9 & certificate_cultivation !=-9 & measure !=-9 & land_nation !=-9 & irrigation !=-9 & aboriginal_seller !=-9 & interest1905 !=-9

* (6) ten and year controlled
logit dian interest1905 paddy_irrig seller_adjacent buyer_adjacent previous_owner measure_avg_3 chi_wei_avg_3 certificate_others_avg_3 certificate_cultivation_avg_3 land_nation e_purple w_rb aboriginal_seller tien_* year_before_1790 year_after_1850 if aboriginal_seller !=-9  & trade <=2 & trade >0 & seller_adjacent !=-9 & buyer_adjacent !=-9 & land!=-9 & land <=2 & previous_owner !=-9 & previous_owner !=9 & certificate_others !=-9 & chi_wei !=-9 & certificate_cultivation !=-9 & measure !=-9 & land_nation !=-9 & w_rb !=-9 & e_purple !=-9 & irrigation !=-9 & year_before_1790 !=-9 & year_after_1850 !=-9 & tien!="-9" & interest1905!=-9
outreg2 using logit_out_governance_all_3_interest_bao.doc, append

* local governance -- bao_decade average + interest; LPM

* (7) LPM
reg dian interest1905 paddy_irrig seller_adjacent buyer_adjacent previous_owner measure_avg_3 chi_wei_avg_3 certificate_others_avg_3 certificate_cultivation_avg_3 land_nation e_purple w_rb aboriginal_seller tien_* year_before_1790 year_after_1850 if aboriginal_seller !=-9  & trade <=2 & trade >0 & seller_adjacent !=-9 & buyer_adjacent !=-9 & land!=-9 & land <=2 & previous_owner !=-9 & previous_owner !=9 & certificate_others !=-9 & chi_wei !=-9 & certificate_cultivation !=-9 & measure !=-9 & land_nation !=-9 & w_rb !=-9 & e_purple !=-9 & irrigation !=-9 & year_before_1790 !=-9 & year_after_1850 !=-9 & tien!="-9" & interest !=-9
outreg2 using reg_out_governance_all_3_interest_bao.doc, replace


log close
